package com.weipai.springboot.module.service.impl;

import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Storage;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.InventoryDao;
import com.weipai.springboot.module.dao.StorageDao;
import com.weipai.springboot.module.mapper.StorageMapper;
import com.weipai.springboot.module.service.StorageService;
import com.weipai.springboot.module.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:50
 */
@Service
public class StorageServiceImpl implements StorageService {

    private static final Logger log = LoggerFactory.getLogger(StorageServiceImpl.class);

    @Autowired
    private StorageDao storagedao;
    @Autowired
    private InventoryDao inventoryDao;

    //保存
    @Override
    public Response save(Storage storage) {
        Response response = new Response();
        if (1 == storagedao.insertSelective(storage)) {
            response.setCode(RetCode.SUCCESS);
        } else {
            log.debug("保存仓储失败:{}", storage.getStorageName());
            response.setCode(RetCode.SAVE_ERROR);
        }
        return response;
    }

    //查询仓储详情
    @Override
    public Response getStorageById(String storageId) {
        Response response = new Response();
        Storage storage = storagedao.getStorageById(storageId);
        if (null != storage) {
            response.setCode(RetCode.SUCCESS);
            response.put("obj", storage);
        } else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //获取数据（不包含逻辑删除的数据）
    @Override
    public Response selectAllByStorageDel(int del, String search, Integer page, Integer size, String sort, String way, String draw) {
        Response response = new Response();
        List<Storage> list = storagedao.selectAllByStorageDel(del, search, sort, way);
        PageInfo<Storage> pageInfo = new PageInfo<>(list);
        if (list.size() > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.NO_DATA);
        }

        PageResult<Storage> pageList = new PageResult<Storage>(list, page, size, (int) pageInfo.getTotal(), draw);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    //批量删除
    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if (storagedao.batchDeleteById(id) > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //修改
    @Override
    public Response update(Storage storage) {
        Response response = new Response();
        if (1 == storagedao.updateByPrimaryKeySelective(storage)) {
            response.setCode(RetCode.SUCCESS);
        } else {
            log.debug("修改品牌失败:{}", storage.getStorageName());
            response.setCode(RetCode.SAVE_ERROR);
        }
        return response;
    }

    //删除
    @Override
    public Response deleteById(String storageId) {
        Response response = new Response();
        Storage storage = storagedao.getStorageById(storageId);
        if (null != storage) {
            storage.setDelTime(DateUtils.timeFormat(System.currentTimeMillis()));
            storage.setStorageDel(-1);
            if (1 == storagedao.updateByPrimaryKey(storage)) {
                response.setCode(RetCode.SUCCESS);
            } else {
                response.setCode(RetCode.REQUEST_ERROR);
            }
        } else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //更改仓储的状态
    @Override
    public Response changeStatus(String storageId, Integer status) {
        Response response = new Response();
        if (1 == storagedao.updateStatus(storageId, status)) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response listNoGoods(String storageId, Integer page, Integer size, String draw) {
        Integer start = (page - 1) * size;
        List<Map<String, Object>> list = storagedao.listNoGoods(storageId, start, size);
        Integer totalNum = storagedao.countNoGoods(storageId);
        PageResult<Map<String, Object>> pageList = new PageResult<>(list, page, size, totalNum, draw);

        Response response = new Response();
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    //根据位置定位仓库
    @Override
    public Response locateStorage(String longitude, String latitude) {
        Response response = new Response();
        Map<String, Object> map = storagedao.locateStorage(longitude, latitude);
        if(null != map && map.size()>0){
            response.setCode(RetCode.SUCCESS);
            response.setData(map);
        }else{
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //查询商品各个仓储下面的库存量
    @Override
    public Response getStorageGoodsNum(String skuNumber) {
        Response response = new Response();
        List<Map<String, Object>> map = storagedao.getStorage();
        if(!map.isEmpty()){
            int size01 = map.size();
            for(int i=0;i<size01;i++){
                Map<String, Object> goodsNum = inventoryDao.getStorageGoodsNum(map.get(i).get("storageId").toString(), skuNumber);
                if(null!=goodsNum){
                    map.get(i).put("goodsNumber", goodsNum.get("goodsNumber"));
                }else{
                    map.get(i).put("goodsNumber", 0);
                }
            }
            response.setCode(RetCode.SUCCESS);
            response.setData(map);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        
        return response;
    }
}
